1 using System;
2 using
UnityEngine;
3
4 namespace
UnityStandardAssets.ImageEffects
5 {
6     
[ExecuteInEditMode]
7     
[RequireComponent (typeof(Camera))]
8     
[AddComponentMenu ("Image Effects/Bloom and Glow/Bloom (Optimized)")]
9     
public class BloomOptimized : PostEffectsBase
10     {
11
12         
public enum Resolution
13         {
14             Low =
0,
15             High =
1,
16         }
17
18         
public enum BlurType
19         {
20             Standard =
0,
21             Sgx =
1,
22         }
23
24         
[Range(0.0f, 1.5f)]
25         
public float threshold = 0.25f;
26         
[Range(0.0f, 2.5f)]
27         
public float intensity = 0.75f;
28
29         
[Range(0.25f, 5.5f)]
30         
public float blurSize = 1.0f;
31
32         Resolution resolution = Resolution.Low;
33         
[Range(1, 4)]
34         
public int blurIterations = 1;
35
36         
public BlurType blurType= BlurType.Standard;
37
38         
public Shader fastBloomShader = null;
39         
private Material fastBloomMaterial = null;
40
41
42         
public override bool CheckResources ()
43         {
44             CheckSupport (
false);
45
46             fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial);
47
48             
if (!isSupported)
49                 ReportAutoDisable ();
50             
return isSupported;
51         }
52
53         
void OnDisable ()
54         {
55             
if (fastBloomMaterial)
56                 DestroyImmediate (fastBloomMaterial);
57         }
58
59         
void OnRenderImage (RenderTexture source, RenderTexture destination)
60         {
61             
if (CheckResources() == false)
62             {
63                 Graphics.Blit (source, destination);
64                 
return;
65             }
66
67             
int divider = resolution == Resolution.Low ? 4 : 2;
68             
float widthMod = resolution == Resolution.Low ? 0.5f : 1.0f;
69
70             fastBloomMaterial.SetVector (
"_Parameter", new Vector4 (blurSize * widthMod, 0.0f, threshold, intensity));
71             source.filterMode = FilterMode.Bilinear;
72
73             
var rtW= source.width/divider;
74             
var rtH= source.height/divider;
75
76             
// downsample
77             RenderTexture rt = RenderTexture.GetTemporary (rtW, rtH,
0, source.format);
78             rt.filterMode = FilterMode.Bilinear;
79             Graphics.Blit (source, rt, fastBloomMaterial,
1);
80
81             
var passOffs= blurType == BlurType.Standard ? 0 : 2;
82
83             
for(int i = 0; i < blurIterations; i++)
84             {
85                 fastBloomMaterial.SetVector (
"_Parameter", new Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshold, intensity));
86
87                 
// vertical blur
88                 RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH,
0, source.format);
89                 rt2.filterMode = FilterMode.Bilinear;
90                 Graphics.Blit (rt, rt2, fastBloomMaterial,
2 + passOffs);
91                 RenderTexture.ReleaseTemporary (rt);
92                 rt = rt2;
93
94                 
// horizontal blur
95                 rt2 = RenderTexture.GetTemporary (rtW, rtH,
0, source.format);
96                 rt2.filterMode = FilterMode.Bilinear;
97                 Graphics.Blit (rt, rt2, fastBloomMaterial,
3 + passOffs);
98                 RenderTexture.ReleaseTemporary (rt);
99                 rt = rt2;
100             }
101
102             fastBloomMaterial.SetTexture (
"_Bloom", rt);
103
104             Graphics.Blit (source, destination, fastBloomMaterial,
0);
105
106             RenderTexture.ReleaseTemporary (rt);
107         }
108     }
109 }


Gõ tìm kiếm nhanh...